<!-- 管理员界面 -->
<title>后台管理员</title>

<div class="layui-card layadmin-header">
	<div class="layui-breadcrumb" lay-filter="breadcrumb">
		<a lay-href="">主页</a>
		<a><cite>学生管理</cite></a>
		<a><cite>学生管理</cite></a>
	</div>
</div>

<!-- 顶部查询 -->
<div class="layui-fluid">
	<div class="layui-card">
		<div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="layadmin-result-formlist">
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">姓名</label>
					<div class="layui-input-block">
						<input type="text" name="name" placeholder="请输入" autocomplete="off" class="layui-input">
					</div>
				</div>

				<div class="layui-inline">
					<button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-result-back-search">
						<i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
					</button>
				</div>
			</div>
		</div>

		<!-- 中部 -->
		<div class="layui-card-body">
			<div style="padding-bottom: 10px;">
				<button class="layui-btn layui-btn-sm" id="addd"><i class="layui-icon layui-icon-chart-screen"></i> 导出到Excel</button>
			</div>

			<!-- 表格开始 -->
			<table id="LAY-result-back-manage" lay-filter="LAY-result-back-manage"></table>




			<script type="text/html" id="table-result-admin">
				<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
        {{#  if(d.role == '超级管理员'){ }}
          <a class="layui-btn layui-btn-disabled layui-btn-xs"><i class="layui-icon layui-icon-delete"></i>删除</a>
        {{#  } else { }}
          <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
        {{#  } }}
      </script>
		</div>
	</div>
</div>

<script>
	layui.use('result', layui.factory('result')).use(['admin', 'result', 'table', 'excel'], function() {
		var $ = layui.$,
			setter = layui.setter,
			admin = layui.admin,
			view = layui.view,
			table = layui.table,
			form = layui.form;

		var excel = layui.excel;

		form.render(null, 'layadmin-result-formlist');

		//监听搜索
		form.on('submit(LAY-result-back-search)', function(data) {
			var field = data.field;

			admin.req({
				type: "post",
				contentType: 'application/json; charset=UTF-8',
				url: setter.api_source + 'result/list',
				data: JSON.stringify({
					"name": field.name
				}),
				beforeSend: function(XMLHttpRequest) {
					//ShowLoading();
				},
				success: function(data, textStatus) {
					console.log(data);
					//执行重载
					table.reload('LAY-result-back-manage', {
						where: field
					});
				},
				complete: function(XMLHttpRequest, textStatus) {
					//HideLoading();
				},
				error: function() {
					//请求出错处理
				}
			});
		});



		// 初始化
		var clazzlist = [];
		var userlist = [];
		var addData = {};

		function init() {
			/**
			 * 请求班级option列表
			 * */
			admin.req({
				type: "post",
				url: setter.api_source + 'clazz/optionlist',
				beforeSend: function(XMLHttpRequest) {
					//ShowLoading();
				},
				success: function(data, textStatus) {
					clazzlist = data.data.options
					addData["clazzlist"] = clazzlist;
				},
				complete: function(XMLHttpRequest, textStatus) {
					//HideLoading();
				},
				error: function() {
					//请求出错处理
				}
			});
			/**
			 * 请求用户用户列表
			 * */
			admin.req({
				type: "post",
				url: setter.api_source + 'sysuser/optionlist',
				beforeSend: function(XMLHttpRequest) {
					//ShowLoading();
				},
				success: function(data, textStatus) {
					userlist = data.data.options
					addData["userlist"] = userlist;

				},
				complete: function(XMLHttpRequest, textStatus) {
					//HideLoading();
				},
				error: function() {
					//请求出错处理
				}
			});
		}
		init();

		$("#addd").click(function(){
			admin.req({
				type: "post",
				url: setter.api_source + 'result/list',
				beforeSend: function(XMLHttpRequest) {},
				success: function(dataa, textStatus) {
					console.log(dataa);
			
					var data = dataa.data.rows;
					var arr = new Array();
					
					var mergeConf = LAY_EXCEL.makeMergeConfig([
						['A1','A2'],
						['B1','B2'],
						['R1','R2'],
						['C1', 'D1', 'E1'],
						['F1', 'G1', 'H1'],
						['I1', 'J1', 'K1'],
						['L1', 'M1', 'N1'],
					]);
					// 2. 配置 列宽 C列宽 150，E列宽150，默认80
					var colConf = LAY_EXCEL.makeColConfig({
						// 'C': 150,
						// 'E': 150,
					}, 80);
					// 3. 第1行行高40，第二行行高30，默认20
					var rowConf = LAY_EXCEL.makeRowConfig({
						1: 40,
						2: 30
					}, 20);
			
					// console.log(data);
			
					var head1 = {}
					head1["1"] = "姓名"
					head1["2"] = "部门"
					
					head1["3"] = "学生评价"
					head1["4"] = ""
					head1["5"] = ""
					
					head1["6"] = "教师自评"
					head1["7"] = ""
					head1["8"] = ""
					
					head1["9"] = "教师互评"
					head1["10"] = ""
					head1["11"] = ""
					
					head1["12"] = "教研室评价"
					head1["13"] = ""
					head1["14"] = ""
					
					head1["15"] = "系部评价"
					head1["16"] = ""
					head1["17"] = ""
					
					head1["18"] = "总分"
					
					
					
					var head2 = {}
					head2["1"] = ""
					head2["2"] = ""
					
					head2["3"] = "应评人数"
					head2["4"] = "实评人数"
					head2["5"] = "得分"
					
					head2["6"] = "应评人数"
					head2["7"] = "实评人数"
					head2["8"] = "得分"
					
					head2["9"] = "应评人数"
					head2["10"] = "实评人数"
					head2["11"] = "得分"
					
					head2["12"] = "应评人数"
					head2["13"] = "实评人数"
					head2["14"] = "得分"
					
					head2["15"] = "应评人数"
					head2["16"] = "实评人数"
					head2["17"] = "得分"
					
					head2["18"] = ""
					
					arr.push(head1)
					arr.push(head2)
			
					for (var i = 0; i < data.length; i++) {
						console.log(data[i].student);
						var tdata = {};
						tdata["1"] = data[i].name
						tdata["2"] = data[i].deptName
						
					
						tdata["3"] = data[i].student.should
						tdata["4"] = data[i].student.reality
						tdata["5"] = data[i].student.score
			
						tdata["6"] = data[i].teacherSelf.should
						tdata["7"] = data[i].teacherSelf.reality
						tdata["8"] = data[i].teacherSelf.score
			
						tdata["9"] = data[i].teacherEach.should
						tdata["10"] = data[i].teacherEach.reality
						tdata["11"] = data[i].teacherEach.score
			
						tdata["12"] = data[i].staff.should
						tdata["13"] = data[i].staff.reality
						tdata["14"] = data[i].staff.score
			
						tdata["15"] = data[i].department.should
						tdata["16"] = data[i].department.reality
						tdata["17"] = data[i].department.score
			
						tdata["18"] = data[i]["score"]
			
						arr.push(tdata);
			
						console.log(tdata);
					}
			
			
					excel.exportExcel({
						sheet1: arr
					}, '评教结果.xlsx', 'xlsx', {
						extend: {
							sheet1: {
								'!merges': mergeConf,
								'!cols': colConf,
								'!rows': rowConf
							}
						}
					});
				},
				complete: function(XMLHttpRequest, textStatus) {
					//HideLoading();
				},
				error: function() {
					//请求出错处理
				}
			});
		});
		
		//事件
		var active = {
			batchdel: function() {
				var checkStatus = table.checkStatus('LAY-result-back-manage'),
					checkData = checkStatus.data; //得到选中的数据
				console.log(checkData);
				if (checkData.length === 0) {
					return layer.msg('请选择数据');
				}

				layer.prompt({
					formType: 1,
					title: '敏感操作，请验证口令'
				}, function(value, index) {
					layer.close(index);

					layer.confirm('确定删除吗？', function(index) {
						var arr = new Array();
						checkData.forEach(function(item) {
							arr.push(item.id);
						})
						console.log(arr);

						admin.req({
							type: "get",
							url: setter.api_source + 'result/batchDelete/' + arr,
							beforeSend: function(XMLHttpRequest) {
								//ShowLoading();
							},
							success: function(data, textStatus) {
								table.reload('LAY-result-back-manage');
								layer.msg('已删除');
							},
							complete: function(XMLHttpRequest, textStatus) {
								HideLoading();
							},
							error: function() {
								//请求出错处理
								layer.msg('删除错误');
							}
						});


					});
				});
			},
			add: function() {
				admin.req({
					type: "post",
					url: setter.api_source + 'result/list',
					beforeSend: function(XMLHttpRequest) {},
					success: function(dataa, textStatus) {
						console.log(dataa);

						var data = dataa.data.rows;
						var arr = new Array();
						
						var mergeConf = LAY_EXCEL.makeMergeConfig([
							['A1','A2'],
							['B1','B2'],
							['R1','R2'],
							['C1', 'D1', 'E1'],
							['F1', 'G1', 'H1'],
							['I1', 'J1', 'K1'],
							['L1', 'M1', 'N1'],
						]);
						// 2. 配置 列宽 C列宽 150，E列宽150，默认80
						var colConf = LAY_EXCEL.makeColConfig({
							// 'C': 150,
							// 'E': 150,
						}, 80);
						// 3. 第1行行高40，第二行行高30，默认20
						var rowConf = LAY_EXCEL.makeRowConfig({
							1: 40,
							2: 30
						}, 20);

						// console.log(data);

						var head1 = {}
						head1["1"] = "姓名"
						head1["2"] = "部门"
						
						head1["3"] = "学生评价"
						head1["4"] = ""
						head1["5"] = ""
						
						head1["6"] = "教师自评"
						head1["7"] = ""
						head1["8"] = ""
						
						head1["9"] = "教师互评"
						head1["10"] = ""
						head1["11"] = ""
						
						head1["12"] = "教研室评价"
						head1["13"] = ""
						head1["14"] = ""
						
						head1["15"] = "系部评价"
						head1["16"] = ""
						head1["17"] = ""
						
						head1["18"] = "总分"
						
						
						
						var head2 = {}
						head2["1"] = ""
						head2["2"] = ""
						
						head2["3"] = "应评人数"
						head2["4"] = "实评人数"
						head2["5"] = "得分"
						
						head2["6"] = "应评人数"
						head2["7"] = "实评人数"
						head2["8"] = "得分"
						
						head2["9"] = "应评人数"
						head2["10"] = "实评人数"
						head2["11"] = "得分"
						
						head2["12"] = "应评人数"
						head2["13"] = "实评人数"
						head2["14"] = "得分"
						
						head2["15"] = "应评人数"
						head2["16"] = "实评人数"
						head2["17"] = "得分"
						
						head2["18"] = ""
						
						arr.push(head1)
						arr.push(head2)

						for (var i = 0; i < data.length; i++) {
							console.log(data[i].student);
							var tdata = {};
							tdata["1"] = data[i].name
							tdata["2"] = data[i].deptName
							
						
							tdata["3"] = data[i].student.should
							tdata["4"] = data[i].student.reality
							tdata["5"] = data[i].student.score

							tdata["6"] = data[i].teacherSelf.should
							tdata["7"] = data[i].teacherSelf.reality
							tdata["8"] = data[i].teacherSelf.score

							tdata["9"] = data[i].teacherEach.should
							tdata["10"] = data[i].teacherEach.reality
							tdata["11"] = data[i].teacherEach.score

							tdata["12"] = data[i].staff.should
							tdata["13"] = data[i].staff.reality
							tdata["14"] = data[i].staff.score

							tdata["15"] = data[i].department.should
							tdata["16"] = data[i].department.reality
							tdata["17"] = data[i].department.score

							tdata["18"] = data[i]["score"]

							arr.push(tdata);

							console.log(tdata);
						}


						excel.exportExcel({
							sheet1: arr
						}, '评教结果.xlsx', 'xlsx', {
							extend: {
								sheet1: {
									'!merges': mergeConf,
									'!cols': colConf,
									'!rows': rowConf
								}
							}
						});
					},
					complete: function(XMLHttpRequest, textStatus) {
						//HideLoading();
					},
					error: function() {
						//请求出错处理
					}
				});
			}
		}
		$('.layui-btn.layuiadmin-btn-admin').on('click', function() {
			var type = $(this).data('type');
			active[type] ? active[type].call(this) : '';
		});
	});
</script>
